//
// Created by LiuDongPeng on 2023/11/16.
//

#include "lowpass_filter.h"



/**
 *
 * @param filter
 * @param alpha
 * @return
 */
int low_pass_filter_init(low_pass_filter_t* filter, float alpha)
{
	if (filter == NULL)
		return -1;

	filter->alpha = alpha;

	return 0;
}

/**
 * @brief Filter work
 * @param[out]	filter
 * @param[in]	val
 * @return
 */
float low_pass_filter_work(low_pass_filter_t* filter, float val)
{
	if (filter == NULL)
		return 0;

	float out;

//	if (filter->first)
//	{
//		filter->lastVal = val;
//		filter->first = false;
//	}
//
//	out = filter->lastVal + filter->alpha * (val - filter->lastVal);
//	filter->lastVal = out;


	out = filter->alpha * val + (1.0f - filter->alpha) * filter->lastVal;
	filter->lastVal = out;

	return out;
}

